%% Visualize EM Lower Bounds
%
%%

% This file is from pmtk3.googlecode.com

function emLogLikelihoodMax
f1 = @(x) log(gaussProb(x,0,0.25.^2)')+1;
f2 = @(x) log(gaussProb(x,1,0.2.^2)')+20;
f3 = @(x) 5*sin(2*(x-0.5))                      + ...
         f1(0.5*x)                              + ...
         f2(0.5*x)                              + ...
         3.5                                    + ...
         20  * gaussProb(x, -2    , 0.5.^2)'     - ...
         20  * gaussProb(x,  3    , 1.^2  )'     - ...
         70  * gaussProb(x,  4    , 0.5.^2)'     + ...
         40  * gaussProb(x, -3    , 0.5.^2)'     + ...
         100 * gaussProb(x, -4    , 0.8.^2)'     + ...
         10  * gaussProb(x,  3    , 0.3.^2)'     - ...
         10  * gaussProb(x, -2.8  , 0.5.^2)';

domain = -5:0.01:5;
figure; hold on;
p1 = plot(domain, f1(domain), '-b' , 'LineWidth', 3);
p2 = plot(domain, f2(domain), ':g' , 'LineWidth', 3);
p3 = plot(domain, f3(domain), '-.r', 'LineWidth', 3);
axis([-3 5 -50 50]);
box on;
set(gca, 'XTick', [], 'YTick', []);
legend([p1, p2, p3], {'Q(\theta,\theta_t)','Q(\theta,\theta_{t+1})',...
  'l(\theta)'}, ...
    'Location', 'NorthWest', 'FontSize', 14);


annotation(gcf,'line',[0.3673 0.3673],[0.5146 0.1108],'LineStyle',':');

%annotation(gcf,'line',[0.4356 0.4356],[0.5976 0.1108],'LineStyle',':');
annotation(gcf,'line',[0.415 0.415],[0.1108 0.59],'LineStyle',':');
 
annotation(gcf,'line',[0.52 0.52],[0.1108 0.7],'LineStyle',':');


annotation(gcf,'textbox',[0.3447 0.02411 0.08859 0.1012],...
  'String',{'\theta_{t}'},...
  'FontSize',16,...
  'FitBoxToText','off',...
  'LineStyle','none');

% [0.4185 0.02411 0.08859 0.1012]
annotation(gcf,'textbox',[0.39 0.02411 0.08859 0.1012],...
  'String',{'\theta_{t+1}'},...
  'FontSize',16,...
  'FitBoxToText','off',...
  'LineStyle','none');

annotation(gcf,'textbox',[0.5 0.02411 0.08859 0.1012],...
  'String',{'\theta_{t+2}'},...
  'FontSize',16,...
  'FitBoxToText','off',...
  'LineStyle','none');

printPmtkFigure emLogLikelihoodMax

end
